<?php
class ResizeComponent extends Object 
{
	//Set foldername which contain image. Default is gallery (..app/webroot/img/gallery)
	var $folder_img = 'gallery';
	//Set image with if you want resize image. Default width is 600px
	var $width = '600';
	//Set image height if you want resize image. Default height is 450px
	var $height = '450';
	//Set image thumbnail width if you want resize image. Default width is 50px
	var $thumb_width = '50';
	//Set image thumbnail height if you want resize image. Default height is 50px
	var $thumb_height = '50';
	//Resize or noresize. Default is no.
	var $hasResize = false;
	//Create thumbnail or no. Default is create.
	var $hasThumb = true;
	
	function startup(&$controller) {
		// This method takes a reference to the controller which is loading it.
	// Perform controller initialization here.
	}
	
	/**
	 * @author hoanbn
	 * @since 18/09/2009
	 * 
	 * @param $image:	name file image
	 * @tutorial: 			
	 */
	
	function processImage($image = null) {
		//set image path
		$destinationMax = realpath(WWW_ROOT.'img'.DS. $this->folder_img.DS) . '/';
		$destinationThumb = realpath(WWW_ROOT.'img'.DS. $this->folder_img .DS.'thumbnail'.DS) . '/';
		
		//create image thumbnail
		if ($this->hasThumb)
		{
			$thumbmin = $this->resize($destinationMax.$image, $this->thumb_width, $this->thumb_height, $destinationThumb .$image);
		}
		
		//Resize image
		if ($this->hasResize)
		{
			$thumbmax = $this->resize($destinationMax.$image, $this->width, $this->height, $destinationMax . $image);
		}
	}
	
	/**
	 * @author hoanbn
	 * @since 18/09/2009
	 * 
	 * @name : resize And CreateThumb
	 * @param $choise 		(0: Noresize, 1: noresize and create thumbnail, 2: Resize, 3: Resize and create thumbnail)
	 * @param $images 		(name image file. Example: example.jpg)
	 * @param $w			(max width image size. Using when you choise resize)
	 * @param $h			(max height image size. Using when you choise resize)
	 * @param $thumb_w		(max width thumbnail image size. Using when you choise resize)
	 * @param $thumb_h		(max height thumbnail image size. Using when you choise resize)
	 * @tutorial Process image upload with optimize you choise
	 * 
	 */
	function resizeAndCreateThumb($choise, $images, $w = null, $h = null, $thumb_w = null, $thumb_h = null)
	{
		//Set width for thumbnail image (Using when you choise create thumbnail)
		$this->thumb_width = $thumb_w;
		//Set height for thumbnail image (Using when you choise create thumbnail)
		$this->thumb_height = $thumb_h;
		//Set width for image (Using when you choise resize)
		$this->width = $w;
		//Set height for image (Using when  you chose resize)
		$this->height = $h;
		
		switch ($choise)
		{
			case 1:
				//Choise no resize
				$this->hasResize = false;
				//Choise create thumbnail
				$this->hasThumb = true;
				//Proccess image
				$this->processImage($images);
				break;
			case 2:
				//Choise resize
				$this->hasResize = true;
				//Choise don't create thumbnail
				$this->hasThumb = false;
				//Process image
				$this->processImage($images);
				break;
			case 3:
				//Choise resize
				$this->hasResize = true;
				//Choise create thumbnail
				$this->hasThumb = true;
				//Process image
				$this->processImage($images);
				break;
			default:
				break;
		}
	}
	
	/**
	 * @name Resize
	 * @param $img			(Full path image source)
	 * @param $w			(Max with resize)
	 * @param $h			(Max height resize)
	 * @param $newfilename	(New name fileimage)
	 * @tutorial Create a new fileimage with size optimize from an image.
	 */
	function resize($img, $w, $h, $newfilename) {
   
       //Check if GD extension is loaded
   
       if (!extension_loaded('gd') && !extension_loaded('gd2')) {
   
        trigger_error("GD is not loaded", E_USER_WARNING);
   
        return false;
   
       }
  
       //Get Image size info
  
       $imgInfo = getimagesize($img);
  
       switch ($imgInfo[2]) {
  
        case 1: $im = imagecreatefromgif($img); break;
  
        case 2: $im = imagecreatefromjpeg($img);  break;
  
        case 3: $im = imagecreatefrompng($img); break;
  
        default:  trigger_error('Unsupported filetype!', E_USER_WARNING);  break;
  
       }
  
       
  
       //If image dimension is smaller, do not resize
  
       if ($imgInfo[0] <= $w && $imgInfo[1] <= $h) {
        $nHeight = $imgInfo[1];  
        $nWidth = $imgInfo[0];
       }else{
		//yeah, resize it, but keep it proportional
        if ($w/$imgInfo[0] > $h/$imgInfo[1]) {
         $nWidth = $w;
  
         $nHeight = $imgInfo[1]*($w/$imgInfo[0]);
  
        }else{
  
         $nWidth = $imgInfo[0]*($h/$imgInfo[1]);
  
         $nHeight = $h;
  
        }
  
       }
  
       $nWidth = round($nWidth);
  
       $nHeight = round($nHeight);
       $newImg = imagecreatetruecolor($nWidth, $nHeight);
       /* Check if this image is PNG or GIF, then set if Transparent*/  
       if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
        imagealphablending($newImg, false);
  
        imagesavealpha($newImg,true);
  
        $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
  
        imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
  
       }
  
       imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);
  
       
  
       //Generate the file, and rename it to $newfilename
  
       switch ($imgInfo[2]) {
  
        case 1: imagegif($newImg,$newfilename); break;
  
        case 2: imagejpeg($newImg,$newfilename);  break;
  
        case 3: imagepng($newImg,$newfilename); break;
  
        default:  trigger_error('Failed resize image!', E_USER_WARNING);  break;
  
       }
  
         
  
         return $newfilename;
  
      }
}
?>